package com.yan.web;

import com.yan.service.Metrics;
import com.yan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author yanheng
 * @data 2020/1/14 16:54
 */
@RestController
public class UserController {

    private Metrics metrics = new Metrics();

    public UserController(){
        metrics.startRepeatReport(10L, TimeUnit.SECONDS);
    }


    @PostMapping(value = "/register")
    public void register(UserVo userVo){
        long startTimestamp = System.currentTimeMillis();
        metrics.recordTimestamp("register", startTimestamp);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long respTime = System.currentTimeMillis() - startTimestamp;
        metrics.recordResponseTime("register", respTime);
    }

    @PostMapping(value = "/login")
    public UserVo login(String telephone, String password){
        long startTimestamp = System.currentTimeMillis();
        metrics.recordTimestamp("login", startTimestamp);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long respTime = System.currentTimeMillis() - startTimestamp;
        metrics.recordResponseTime("login", respTime);
        return null;
    }
}
